<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '330px'
    },
    tag: {
      type: String,
      default: new Array()
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {

      this.chart = echarts.init(this.$el, 'macarons')
      //生成单纯
      let NAME=[];
      for(let i=0;i<this.tag.length;i++){
        NAME.push(this.tag[i].tag_name)
      }
      let DATA=[];
      for(let i=0;i<this.tag.length;i++){
        DATA.push(this.tag[i].value)
      }
      let options  = {
        title: {
          text: '主要TAG'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          type: 'category',
          data: NAME
        },
        series: [
          {
            name: '留言数',
            type: 'bar',
            data: DATA,
            label: {
              show: true, // 显示数值
              position: 'right' // 数值显示的位置
            }
          },

        ]
      };
      this.chart.setOption(options)
    }
  }
}
</script>
